public class ProductOfArrayExceptSelf {
    // 除自身以外数组乘积 https://leetcode.cn/problems/product-of-array-except-self/
    public int[] productExceptSelf(int[] nums) {
        //前缀
        int[] prefix=new int[nums.length];
        prefix[0]=1;
        for(int i=1;i<nums.length;i++) {
            prefix[i]=prefix[i-1]*nums[i-1];
        }
        //后缀
        int[] suffix=new int[nums.length];
        suffix[nums.length-1]=1;
        for(int i=nums.length-2;i>=0;i--) {
            suffix[i]=suffix[i+1]*nums[i+1];
        }
        //计算乘积
        int[] ret=new int[nums.length];
        for(int i=0;i<nums.length;i++) {
            ret[i]=prefix[i]*suffix[i];
        }
        return ret;
    }
}
